package com.javatiaocao.myblog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatiaocao.myblog.mapper.LeaveWordMapper;
import com.javatiaocao.myblog.mapper.UserMapper;
import com.javatiaocao.myblog.model.LeaveMessage;
import com.javatiaocao.myblog.model.User;
import com.javatiaocao.myblog.service.LeaveWordService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.TimeUtil;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class LeaveWordServiceImpl implements LeaveWordService {

    @Autowired
    private LeaveWordMapper leaveWordMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public DataMap
    newLeaveWord(Integer rows, Integer pageNum) {
        HashMap<String, Object> dataMap = new HashMap<>();
        HashMap<String, Object> pageInfo = new HashMap<>();
        //分页插件开启
        PageHelper.startPage(pageNum, rows);

        //获取评论
        List<LeaveMessage> leaveMessageList = leaveWordMapper.getnewLeaveWord();
        PageInfo<LeaveMessage> page = new PageInfo<>(leaveMessageList);

        //rows:data['data']['pageInfo']['pageSize'],//每页显示条数
        //pageNum:data['data']['pageInfo']['pageNum'],//当前所在页码
        //pages:data['data']['pageInfo']['pages'],//总页数
        //total:data['data']['pageInfo']['total'],//总记录数
        pageInfo.put("pageSize",page.getPageSize());
        pageInfo.put("pageNum",page.getPageNum());
        pageInfo.put("pages",page.getPages());
        pageInfo.put("total",page.getTotal());

        ArrayList<HashMap> result = new ArrayList<>();

        for (LeaveMessage leaveMessage : leaveMessageList) {
            HashMap<String, Object> object = new HashMap<>();
            object.put("id",leaveMessage.getId());
            object.put("pagePath",leaveMessage.getPageName());
            object.put("leaveWordContent",leaveMessage.getLeaveMessageContent());
            User user = userMapper.selectUserById(leaveMessage.getAnswererId());
            if (user != null) {
                object.put("answerer",user.getUsername());
            }else {
                object.put("answerer","已注销用户");
            }
            object.put("leaveWordDate",leaveMessage.getLeaveMessageDate());
            result.add(object);
        }
        dataMap.put("result",result);
        dataMap.put("pageInfo",pageInfo);

        return DataMap.success().setData(dataMap);
    }


    /**
     *
     * @param pageName
     * @return
     */
    @Override
    public DataMap getPageLeaveMessage(String pageName) {

        HashMap<String, Object> returnHashMap = new HashMap<>();
        //data['result'], function (index,obj)
        List<LeaveMessage> leaveMessageList = leaveWordMapper.getPageLeaveMessage(pageName);
        ArrayList<JSONObject> result = new ArrayList<>();
        for (LeaveMessage leaveMessage : leaveMessageList) {
            JSONObject object = new JSONObject();
            User user = userMapper.selectUserById(leaveMessage.getAnswererId());//获取留言者信息
            if (user == null) {
                object.put("avatarImgUrl","www.javatiaocao.com");
                object.put("answerer","已注销用户");
            }else {
                object.put("answerer",user.getUsername());
                object.put("avatarImgUrl",user.getAvatarImgUrl());
            }
            //obj:id,avatarImgUrl,answerer,leaveMessageDate,leaveMessageContent,likes,isLiked,replies
            object.put("id",leaveMessage.getId());
            object.put("leaveMessageDate",leaveMessage.getLeaveMessageDate());
            object.put("leaveMessageContent",leaveMessage.getLeaveMessageContent());
            object.put("likes",leaveMessage.getLikes());
            object.put("isLiked",0);

            //obj['replies'],function (index1,obj1)
            //id,answerer,respondent,leaveMessageContent,leaveMessageDate,
            //获取留言的留言
            int repliesNum = leaveWordMapper.getRepliesNum(leaveMessage.getId(),leaveMessage.getPageName());
            ArrayList<HashMap> repliesHashMaps = new ArrayList<>();
            if (repliesNum != 0) {
                List<LeaveMessage> repliesList = leaveWordMapper.getReplies(leaveMessage.getId(),leaveMessage.getPageName());
                for (LeaveMessage repliesMessage : repliesList) {
                    HashMap<String, Object> repliesObject = new HashMap<>();
                    repliesObject.put("id",repliesMessage.getId());
                    User answereruser = userMapper.selectUserById(repliesMessage.getRespondentId());//回复者信息
                    if (answereruser == null) {
                        repliesObject.put("answerer","已注销用户");
                    }else {
                        repliesObject.put("answerer",answereruser.getUsername());
                    }
                    User repliesUser = userMapper.selectUserById(repliesMessage.getAnswererId());//被回复者信息
                    if (repliesUser == null) {
                        repliesObject.put("respondent","已注销用户");
                    }else {
                        repliesObject.put("respondent",repliesUser.getUsername());
                    }
                    repliesObject.put("leaveMessageContent",repliesMessage.getLeaveMessageContent());
                    repliesObject.put("leaveMessageDate",repliesMessage.getLeaveMessageDate());
                    repliesHashMaps.add(repliesObject);
                }
            }
            object.put("replies",repliesHashMaps);
            result.add(object);
        }
        returnHashMap.put("result",result);


        return DataMap.success().setData(returnHashMap);
    }

    @Override
    public DataMap publishLeaveMessage(LeaveMessage leaveMessage, String parentId, String respondent, UserDetails userDetails) {
        leaveMessage.setPId(Integer.valueOf(parentId));
        User user = userMapper.findUserByPhone(userDetails.getUsername());
        if (user == null) {
            leaveMessage.setAnswererId(1);
            leaveMessage.setRespondentId(1);
        }else {
            leaveMessage.setAnswererId(user.getId());
            leaveMessage.setRespondentId(user.getId());
        }
        TimeUtil timeUtil = new TimeUtil();
        leaveMessage.setLeaveMessageDate(timeUtil.getFormatDateForFive());
        leaveMessage.setLikes(0);
        leaveMessage.setIsRead(1);
        leaveWordMapper.publishLeaveMessage(leaveMessage);
        //answerer,respondent,leaveMessageContent,leaveMessageDate
        HashMap<String, Object> data = new HashMap<>();
        data.put("answerer",leaveMessage.getAnswererId());
        data.put("respondent",leaveMessage.getRespondentId());
        data.put("leaveMessageContent",leaveMessage.getLeaveMessageContent());
        data.put("leaveMessageDate",leaveMessage.getLeaveMessageDate());
        return DataMap.success().setData(data);
    }
}
